package dl.bukkit.myststus.utils;

import dl.bukkit.myststus.hook.PlaceholderAPIHook;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.List;

public class StringUtils {

    private StringUtils() {
    }

    public static double toScale(Player player, String thisValue, String maxValue) {
        String str = PlaceholderAPIHook.toString(player, thisValue);
        double scale;
        if (str.lastIndexOf("%") == str.length() - 1) {
            scale = NumberUtils.toDouble(str.substring(0, str.lastIndexOf("%"))) * 0.01;
        }else {
            String max = PlaceholderAPIHook.toString(player, maxValue);
            scale = (NumberUtils.toDouble(str) / NumberUtils.toDouble(max));
        }
        return scale;
    }

    private static final List<String> colors = Arrays.asList(
            "§a", "§b", "§c", "§d", "§e", "§f", "§l", "§m", "§r", "§n",
            "§0", "§1", "§2", "§3", "§4", "§5", "§6", "§7", "§8", "§9"
    );

    public static String delAllColors(String str) {
        String s = str;
        for (String color : colors) {
            s = s.replaceAll(color, "");
        }
        return s;
    }
}
